package com.deparse.dpframe.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.deparse.dpframe.common.DPConsts;
import com.deparse.dpframe.ui.dialog.DProgressDialog;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hsy on 2016/4/21.
 */
public class DPCommonUtil {

    private static SharedPreferences getDPSharedPreferences(Context context) {
        return getSharedPreferences(context, DPConsts.SHARED_PRENCE_NAME_FOR_DPFRAME, Context.MODE_APPEND);
    }

    private static SharedPreferences getDPSharedPreferences(Context context, int mode) {
        return getSharedPreferences(context, DPConsts.SHARED_PRENCE_NAME_FOR_DPFRAME, mode);
    }

    public static SharedPreferences getSharedPreferences(Context context, String name, int mode) {
        return context.getSharedPreferences(name, mode);
    }

    public static boolean getIsFirstInUse(Context context) {
        return getDPSharedPreferences(context).getBoolean(DPConsts.IS_APP_FIRST_IN_USE, true);
    }

    public static boolean setIsFirstInUse(Context context, boolean isFirstInUse) {
        return getDPSharedPreferences(context).edit().putBoolean(DPConsts.IS_APP_FIRST_IN_USE, isFirstInUse).commit();
    }

    public static boolean setLastSavedVersionCode(Context context, int verCode) {
        return getDPSharedPreferences(context).edit().putInt(DPConsts.LAST_SAVED_VERSION_CODE, verCode).commit();
    }

    public static int getLastSavedVersionCode(Context context) {
        return getDPSharedPreferences(context).getInt(DPConsts.LAST_SAVED_VERSION_CODE, 0);
    }

    public static int getInternalVersionCode(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private static DProgressDialog mUploadProgressDialog = null;

    public static void showUploadProgressDialog(Context context, String msg) {
        if (null == mUploadProgressDialog || !mUploadProgressDialog.isShowing()) {
            mUploadProgressDialog = new DProgressDialog(context, msg);
            mUploadProgressDialog.setCancelable(false);
            mUploadProgressDialog.setCanceledOnTouchOutside(false);
            mUploadProgressDialog.show();
        }
    }

    public static void hideUploadProgressDialog() {
        if (null != mUploadProgressDialog) {
            mUploadProgressDialog.dismiss();
            mUploadProgressDialog = null;
        }
    }
}
